﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Lcygcc.Domain.TaskService
{
    /// <summary>
    /// 线程帮助类
    /// </summary>
    public class ThreadHelper
    {
        public static void StartBgThread(ref Thread t,ParameterizedThreadStart pts,object state,bool killAlive)
        {
            if (t != null)
            {
                if(t.IsAlive)
                {
                    if (killAlive)
                    {
                        t.Interrupt();
                        t.Abort();
                        t.Join();
                    }
                    else
                    {
                        return;
                    }
                }
                if (t.ThreadState.ToString().Contains(ThreadState.Stopped.ToString())
                    || t.ThreadState.ToString().Contains(ThreadState.Aborted.ToString())
                    || t.ThreadState.ToString().Contains(ThreadState.AbortRequested.ToString()))
                {
                    t = new Thread(pts);
                }
            }
            else
            {
                t = new Thread(pts);
            }
            t.IsBackground = true;
            t.Start(state);
        }
    }
}
